package com.atguigu.case1;

import org.apache.zookeeper.*;

import java.io.IOException;

public class DistributeServer {
    private String connectString="192.168.80.130:2181,192.168.80.131:2181,192.168.80.132:2181";
    private Integer sessionTimeout=2000;
    public static void main(String[] args) throws Exception {
        DistributeServer ds=new DistributeServer();
        //1、获取zk连接
        ZooKeeper zooKeeper = ds.getconnect();
        //2、注册服务器zk集群
        ds.regist(zooKeeper,"server01","server-01");
        //3、启动业务（sleep）
        ds.business();
    }
    private ZooKeeper getconnect() throws IOException {
        //1、获取zk连接
        ZooKeeper zk=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
        return zk;
    }
    public void regist(ZooKeeper zk,String hostname,String value) throws Exception {
        //2、注册服务器zk集群
        String create = zk.create("/servers/"+hostname, value.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(create+"上线完成！");
    }
    public void business() throws InterruptedException {
        //3、启动业务（sleep）
        Thread.sleep(1000000);
    }
}
